package com.smh.yunpicturebackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @author shiminghui
 * @date 2025/6/23 23:59
 * @description: 异步配置类
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    // 自定义线程池
    @Bean(name = "lightTaskExecutor")
    public Executor lightTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(10);
        threadPoolTaskExecutor.setQueueCapacity(100);
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true); // 开启核心线程回收
        threadPoolTaskExecutor.setKeepAliveSeconds(30); // 所有线程空闲30秒后回收
        threadPoolTaskExecutor.setThreadNamePrefix("Async-light");
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

    @Bean(name = "heavyTaskExecutor")
    public Executor heavyTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(10);
        threadPoolTaskExecutor.setQueueCapacity(100);
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true); // 开启核心线程回收
        threadPoolTaskExecutor.setKeepAliveSeconds(30); // 所有线程空闲30秒后回收
        threadPoolTaskExecutor.setThreadNamePrefix("Async-heavy");
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }


}
